{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "source": [
    "# Image Segmentation with Grounded SAM\n",
    "\n",
    "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/geoai/blob/main/docs/examples/grounded_sam.ipynb)\n",
    "\n",
    "This notebook demonstrates how to use the `GroundedSAM` class to segment images using Grounded SAM. To learn more about Grounded SAM, please refer to the [Grounded SAM](https://huggingface.co/docs/transformers/model_doc/grounding-dino#grounded-sam) documentation.\n",
    "\n",
    "## Install package\n",
    "\n",
    "To use the `geoai-py` package, ensure it is installed in your environment. Uncomment the command below if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# %pip install geoai-py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import library"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import geoai\n",
    "from geoai.segment import GroundedSAM"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Download sample data\n",
    "\n",
    "The sample data is from [OpenAerialMap](https://map.openaerialmap.org/#/-122.24462628364563,37.54457094085014,18/square/023010230021/60a48baa25820c0005e45906?_k=jqrisj). Credits to the provider Jason McMinn. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "raster_url = \"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/aerial.tif\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "raster_path = geoai.download_file(raster_url)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualize sample data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_raster(raster_url)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Image Segmentation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "grounded_sam = GroundedSAM(\n",
    "    detector_id=\"IDEA-Research/grounding-dino-tiny\",\n",
    "    segmenter_id=\"facebook/sam-vit-base\",\n",
    "    device=None,  # Will auto-detect CUDA if available\n",
    "    tile_size=1024,\n",
    "    overlap=128,\n",
    "    threshold=0.3,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "text_prompts = [\"building\", \"car\", \"tree\"]\n",
    "output_file = \"segmented_objects.tif\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result_files = grounded_sam.segment_image(\n",
    "    input_path=raster_path,\n",
    "    output_path=output_file,\n",
    "    text_prompts=text_prompts,\n",
    "    polygon_refinement=True,\n",
    "    export_boxes=True,\n",
    "    export_polygons=True,\n",
    "    smoothing_sigma=1.0,\n",
    "    nms_threshold=0.5,\n",
    "    min_polygon_area=50,\n",
    "    simplify_tolerance=1.0,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result_files"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualize results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "polygons = result_files[\"polygons\"]\n",
    "boxes = result_files[\"boxes\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_vector_interactive(\n",
    "    polygons, basemap=raster_url, column=\"label\", cmap=\"tab20\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_raster(output_file, indexes=[2], basemap=raster_url, layer_name=\"Building\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_raster(output_file, indexes=[3], basemap=raster_url, layer_name=\"Car\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_raster(output_file, indexes=[4], basemap=raster_url, layer_name=\"Tree\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "geo",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
